// import api from './api'
import axios from 'axios'

const service = axios.create({
    baseURL: 'http://localhost:3000',
    // timeout: 5000 // 请求超时时间
  })


async function Services(data,postData) {

    if(data.method == 'get'){
        const ret = await service({
            url:data.url,
            method: data.method,
        })
        return ret;
    }else{
        if(data.method == 'post'){
            let newData = {};
            let timestamps = Date.now();
            if(data.isParams && data){
               newData = new FormData();
                 newData.append('timestamp',timestamps)
                for(let key in postData){
                    newData.append(key,postData[key])
                    // console.log(newData)
                }
            }else{
                newData = postData;
                let tem = {
                    timestamp: timestamps
                  };
                newData = { ...postData, ...tem };
                console.log(newData)
            }
            const ret = await service({
                url:data.url,
                method: data.method,
                data:newData
            })
            console.log(ret.data.result)
            return ret;
        }
    }
    // 添加请求拦截器
    service.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    console.log(config)
    return config;
  }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  });

// 添加响应拦截器
service.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    console.log(response)
    return response;
  }, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
  });

    }


export default Services;